package com.test.lab.redis.jedis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisUtil {

    private static Logger LOG = LoggerFactory.getLogger(JedisUtil.class);

    private static JedisPool jedisPool = null;

    private JedisUtil() {

    }

    private static final String REDIS_HOST = "10.10.13.12";
    private static final String REDIS_PORT = "6379";
    private static final String REDIS_PASS = "123456";
    private static final String REDIS_TIMEOUT = "10000";
    private static final String REDIS_MAXIDLE = "300";
    private static final String REDIS_MAXTOTAL = "600";
    private static final String REDIS_MAXWAIL = "1000";
    private static final String REDIS_TESTONBORROW = "false";




    //写成静态代码块形式，只加载一次，节省资源
    static {
//        Properties properties = PropertiesUtil.loadProperties("redis.properties");
//        String host = properties.getProperty("redis.host");
//        String port = properties.getProperty("redis.port");
//        String pass = properties.getProperty("redis.pass");
//        String timeout = properties.getProperty("redis.timeout");
//        String maxIdle = properties.getProperty("redis.maxIdle");
//        String maxTotal = properties.getProperty("redis.maxTotal");
//        String maxWaitMillis = properties.getProperty("redis.maxWaitMillis");
//        String testOnBorrow = properties.getProperty("redis.testOnBorrow");

        String host =REDIS_HOST;
        String port = REDIS_PORT;
        String pass = REDIS_PASS;
        String timeout = REDIS_TIMEOUT;
        String maxIdle = REDIS_MAXIDLE;
        String maxTotal = REDIS_MAXTOTAL;
        String maxWaitMillis = REDIS_MAXWAIL;
        String testOnBorrow = REDIS_TESTONBORROW;

        JedisPoolConfig config = new JedisPoolConfig();
        //控制一个pool可分配多少个jedis实例，通过pool.getResource()来获取；
        //如果赋值为-1，则表示不限制；如果pool已经分配了maxActive个jedis实例，则此时pool的状态为exhausted(耗尽)。
        config.setMaxTotal(Integer.parseInt(maxTotal));
        //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
        config.setMaxIdle(Integer.parseInt(maxIdle));
        //表示当borrow(引入)一个jedis实例时，最大的等待时间，如果超过等待时间，则直接抛出JedisConnectionException；
        config.setMaxWaitMillis(Long.parseLong(maxWaitMillis));
        //在borrow一个jedis实例时，是否提前进行validate操作；如果为true，则得到的jedis实例均是可用的；
        config.setTestOnBorrow(Boolean.valueOf(testOnBorrow));

        jedisPool = new JedisPool(config, host, Integer.parseInt(port), Integer.parseInt(timeout), pass);
    }

    /**
     * 从jedis连接池中获取获取jedis对象
     *
     * @return
     */
    private Jedis getJedis() {
        return jedisPool.getResource();
    }

    private static final JedisUtil jedisUtil = new JedisUtil();

    /**
     * 获取JedisUtil实例
     *      *
     * @return
     */
    public static JedisUtil getInstance() {
        return jedisUtil;
    }

    /**
     * 回收jedis(放到finally中)
     *
     * @param jedis
     */
    private void returnJedis(Jedis jedis) {
        if (null != jedis && null != jedisPool) {
            jedisPool.returnResource(jedis);
        }
    }

    public String get(String key) {
        Jedis jedis = null;
        try {
            System.out.println("开始");
            LOG.error("redis get，key:{}",key);
            jedis = getJedis();
            String result = jedis.get(key);
            LOG.info("redis get，key:{},result:{}",key, result);
            return result;
        } catch (Exception e) {
            LOG.error("redis get发送异常，key:{},异常为：{}",key, e);
            e.printStackTrace();
            return null;
        } finally {
            returnJedis(jedis);
        }
    }

    public static void main(String[] args) {
        JedisUtil jedisUtil = JedisUtil.getInstance();
        String result = jedisUtil.get("aa");
    }



}
